package com.segi.view.xlistview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.segi.view.R;

/**
 * The footer view for {@link com.segi.view.xlistview.XListView} and
 * {@link com.segi.view.xlistview.XScrollView}
 *
 * @author markmjw
 * @date 2013-10-08
 */
public class XFooterView extends LinearLayout {
    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_LOADING = 2;

    private final int ROTATE_ANIM_DURATION = 180;

    private View mLayout;
    private View mProgressBar;
    private TextView mHintView;
    private ImageView mHintImage;
    private Animation mRotateUpAnim;
    private Animation mRotateDownAnim;

    private int mState = STATE_NORMAL;
    private int mFooterType = 2;

    public final static int COMMON_FOOTER = 1;//默认

    public final static int WINDWILL_FOOTER = 2;//大风车
    private AnimationDrawable mProgressAnim;

    public XFooterView(Context context) {
        super(context);
        initView(context);
    }

    public XFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {

        if (mFooterType == COMMON_FOOTER) {
            mLayout = LayoutInflater.from(context).inflate(R.layout.vw_footer_v2_common, null);
            mLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            addView(mLayout);

            mProgressBar = mLayout.findViewById(R.id.footer_progressbar);
            mHintView = (TextView) mLayout.findViewById(R.id.footer_hint_text);
            mHintImage = (ImageView) mLayout.findViewById(R.id.footer_arrow);

            mRotateUpAnim = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
            mRotateUpAnim.setFillAfter(true);

            mRotateDownAnim = new RotateAnimation(180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
            mRotateDownAnim.setFillAfter(true);
        } else if (mFooterType == WINDWILL_FOOTER) {
            mLayout = LayoutInflater.from(context).inflate(R.layout.vw_footer_v2_windwill, null);
            mLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            addView(mLayout);
            mProgressBar = mLayout.findViewById(R.id.footer_progressbar);
            mHintView = (TextView) mLayout.findViewById(R.id.footer_hint_text);
            mProgressAnim = (AnimationDrawable) ((ImageView) mProgressBar).getDrawable();
        }

    }

    /**
     * Set footer view state
     *
     * @param state
     * @see #STATE_LOADING
     * @see #STATE_NORMAL
     * @see #STATE_READY
     */
    public void setState(int state) {
        if (state == mState) {
            return;
        }


        if (mFooterType == COMMON_FOOTER) {
            if (state == STATE_LOADING) {
                mHintImage.clearAnimation();
                mHintImage.setVisibility(View.INVISIBLE);
                mProgressBar.setVisibility(View.VISIBLE);
                mHintView.setVisibility(View.INVISIBLE);
            } else {
                mHintView.setVisibility(View.VISIBLE);
                mHintImage.setVisibility(View.VISIBLE);
                mProgressBar.setVisibility(View.INVISIBLE);
            }

            switch (state) {
                case STATE_NORMAL:
                    if (mState == STATE_READY) {
                        mHintImage.startAnimation(mRotateDownAnim);
                    }
                    if (mState == STATE_LOADING) {
                        mHintImage.clearAnimation();
                    }
                    mHintView.setText(R.string.footer_hint_load_normal);
                    break;

                case STATE_READY:
                    if (mState != STATE_READY) {
                        mHintImage.clearAnimation();
                        mHintImage.startAnimation(mRotateUpAnim);
                        mHintView.setText(R.string.footer_hint_load_ready);
                    }
                    break;

                case STATE_LOADING:
                    break;
            }
        }

        /**
         * 大风车的底部
         */
        else if (mFooterType == WINDWILL_FOOTER) {
            if (state == STATE_LOADING) {
//                mProgressBar.setVisibility(View.VISIBLE);
//                mHintView.setVisibility(View.INVISIBLE);
                if (!mProgressAnim.isRunning()) {
                    mProgressAnim.start();
                }
            } else {
//                mProgressBar.setVisibility(View.INVISIBLE);
//                mHintView.setVisibility(View.VISIBLE);
                if (mProgressAnim.isRunning()) {
                    mProgressAnim.stop();
                }
            }

            switch (state) {
                case STATE_NORMAL:
                    if (mState == STATE_READY) {
                        mProgressBar.setVisibility(GONE);
                        mHintView.setVisibility(View.VISIBLE);
                        if (mState != STATE_READY) {
                            mHintView.setText(R.string.footer_hint_load_normal);
                        }
                    }
                    if (mState == STATE_LOADING) {
                        mProgressBar.setVisibility(VISIBLE);
                        if (mProgressAnim.isRunning()) {
                            mProgressAnim.stop();
                        }
                    }
                    mHintView.setText(R.string.footer_hint_load_normal);
                    break;

                case STATE_READY:
                    if (mState != STATE_READY) {
                        mHintView.setText(R.string.footer_hint_load_ready);
                    }
                    break;

                case STATE_LOADING:
                    if (mState != STATE_LOADING) {
                        mHintView.setText(R.string.footer_hint_load_loading);
                    }

                    mProgressBar.setVisibility(VISIBLE);
                    if (mState != STATE_READY) {
                        if (!mProgressAnim.isRunning()) {
                            mProgressAnim.start();
                        }
                    }
                    break;
            }
        }


        mState = state;
    }

    /**
     * Set footer view bottom margin.
     *
     * @param margin
     */
    public void setBottomMargin(int margin) {
        if (margin < 0) return;
        LayoutParams lp = (LayoutParams) mLayout.getLayoutParams();
        lp.bottomMargin = margin;
        mLayout.setLayoutParams(lp);
    }

    /**
     * Get footer view bottom margin.
     *
     * @return
     */
    public int getBottomMargin() {
        LayoutParams lp = (LayoutParams) mLayout.getLayoutParams();
        return lp.bottomMargin;
    }

    /**
     * normal status
     */
    public void normal() {
        mHintView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.GONE);
    }

    /**
     * loading status
     */
    public void loading() {
        mHintView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
    }

    /**
     * hide footer when disable pull load more
     */
    public void hide() {
        LayoutParams lp = (LayoutParams) mLayout.getLayoutParams();
        lp.height = 0;
        mLayout.setLayoutParams(lp);
    }

    /**
     * show footer
     */
    public void show() {
        findViewById(R.id.normal_layout).setVisibility(VISIBLE);
        findViewById(R.id.bottom_line_view).setVisibility(GONE);
        LayoutParams lp = (LayoutParams) mLayout.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        mLayout.setLayoutParams(lp);
    }

    /**
     * show footer
     */
    public void showBottomLine() {
        findViewById(R.id.normal_layout).setVisibility(GONE);
        findViewById(R.id.bottom_line_view).setVisibility(VISIBLE);
        LayoutParams lp = (LayoutParams) mLayout.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        mLayout.setLayoutParams(lp);
    }

}
